<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/8
 * Time: 15:33
 */

namespace App\Services\Admin;

use Facades\App\Repositories\Eloquent\ManageRepositoryEloquent;
use Facades\Yajra\Datatables\Html\Builder;
use App\Traits\DatatableActionButtonTrait;
use Datatables;
use Exception;

class ManageService
{
    use DatatableActionButtonTrait;

    protected $module = 'manage';

    protected $indexRoute = 'manage.index';

    protected $createRoute = 'manage.create';

    protected $showRoute = 'manage.show';

    protected $editRoute = 'manage.edit';

    protected $destroyRoute = 'manage.destroy';

    /**
     * 首页
     * @return array
     */
    public function index()
    {
        if (request()->ajax()) {
            return $this->ajaxData();
        }

        $html = Builder::parameters([
            'searchDelay' => 350,
            'language' => [
                'url' => url(getThemeAssets('dataTables/language/zh.json', true))
            ],
            'drawCallback' => <<<Eof
					function() {
				        LaravelDataTables["dataTableBuilder"].$('.tooltips').tooltip( {
				          placement : 'top',
				          html : true
				        });
			        },
Eof
        ])->addIndex(['data' => 'DT_Row_Index', 'name' => 'DT_Row_Index', 'title' => trans('common.number')])
            ->addColumn(['data' => 'real_name', 'name' => 'real_name', 'title' => trans('manage.real_name')])
            ->addColumn(['data' => 'mobile', 'name' => 'mobile', 'title' => trans('manage.mobile')])
            ->addColumn(['data' => 'ID_cards', 'name' => 'ID_cards', 'title' => trans('manage.ID_cards')])
            ->addColumn(['data' => 'sex', 'name' => 'sex', 'title' => trans('manage.sex')])
            ->addColumn(['data' => 'authentication_status', 'name' => 'authentication_status', 'title' => trans('manage.authentication_status')])
            ->addColumn(['data' => 'add_time', 'name' => 'add_time', 'title' => trans('manage.add_time')])
            ->addColumn(['data' => 'status', 'name' => 'status', 'title' => trans('manage.status')])
            ->addAction(['data' => 'action', 'name' => 'action', 'title' => trans('common.action')]);
        return compact('html');
    }

    /**
     * datatable数据
     * @return mixed
     */
    public function ajaxData()
    {
        $getData = ManageRepositoryEloquent::all();
        foreach ($getData as $key=>$val){
            $getData[$key]['sex'] = getSexByUser($val['sex']);
            $getData[$key]['status'] = getStatusByUser($val['status']);
            $getData[$key]['authentication_status'] = getAuthenticationStatusByUser($val['status']);
            $getData[$key]['add_time'] = getAddTimeByUser($val['add_time']);
        }
        return Datatables::of($getData)
            ->addIndexColumn()
            ->addColumn('action', function ($permission)
            {
                return $this->getActionButtonAttribute($permission->id);
            })
            ->make(true);
    }

    /**
     * 重写datatable action按钮
     * @param $id
     * @return string
     */
    public function getActionButtonAttribute($id)
    {
        return $this->getShowActionButtion($id).
            $this->getEditActionButton($id).
            $this->getDestroyActionButton($id);
    }
}